//Tracker.vn
//CopyRight By Tracker Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using tracker.Models;
using tracker.Models.DTO;
namespace tracker.Models.DAL
{
    public class UserLoginDAL : ErpCatalogueBaseBL
    {
        #region Properties Define
        private string FIELD_USERNAME = "Username";
        private string FIELD_PARENTCODE = "ParentCode";
        private string FIELD_SECURITYCODE = "SecurityCode";
        private string FIELD_PASSWORD = "Password";
        #endregion

        #region STORE NAME
        public const string USERLOGIN_SELECTBYUSERNAME = "UserLogin_SelectByUsername";
        public const string USERLOGIN_SELECTBYPARENTCODE = "UserLogin_SelectByParentCode";
        public const string USERLOGIN_SELECTBYSECURITYCODE = "UserLogin_SelectBySecurityCode";
        public const string USERLOGIN_UPDATEGROUP = "UserLogin_UpdateGroup";
        public const string USERLOGIN_INSERT = "UserLogin_Insert";
        public const string USERLOGIN_SUMDEVICEBYSECURITYCODE = "UserLogin_SumDeviceBySecurityCode";
        public const string USERLOGIN_DELETEUSER = "UserLogin_DeleteUser";
        public const string USERLOGIN_DELETEMULTIPUSER = "UserLogin_DeleteMultipUser";
        public const string USERLOGIN_UPDATEPASSWORD = "UserLogin_UpdatePassword";
        public const string USERLOGIN_UPDATE = "UserLogin_Update";
        public const string USERLOGIN_SELECTALLBYADMIN = "UserLogin_SelectAllByAdmin";
        private const string USERLOGIN_CHANGEPARENT = "UserLogin_ChangeParent";
        #endregion

        public EUserLogin getByUserName(string userName)
        {
            EUserLogin user = null;
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(USERLOGIN_SELECTBYUSERNAME,
                                                                 new string[] { FIELD_USERNAME },
                                                                 new object[] { userName });
            
            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                user = EUserLogin.createUser(dt.Rows[0]);
            }
            return user;
        }
        public List<EUserLogin> getUserLoginByParentCode(string parentCode)
        {
            List<EUserLogin> lstData = null;
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(USERLOGIN_SELECTBYPARENTCODE,
                                                                  new string[] { FIELD_PARENTCODE },
                                                                  new object[] { parentCode });
            
            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                lstData = new List<EUserLogin>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    EUserLogin user = EUserLogin.createUser(dt.Rows[i]);
                    lstData.Add(user);
                }

            }
            return lstData;
        }
        public List<EUserLogin> getUserLoginBySecurityCode(string securityCode)
        {
            List<EUserLogin> lstData = null; ;
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(USERLOGIN_SELECTBYSECURITYCODE,
                                                                  new string[] { FIELD_SECURITYCODE },
                                                                  new object[] { securityCode });
           
            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                lstData = new List<EUserLogin>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    EUserLogin user = EUserLogin.createUser(dt.Rows[0]);
                    lstData.Add(user);
                }

            }
            return lstData;
        }
        public string UpdateGroup(EUserLogin OUserLogin)
        {
            string [] addFieldName = new string[]{
                                                "Fullname",
                                                "Address",
                                                "Phone",
                                                "Cellphone",
                                                "ModifyBy",
                                                "ModifyDate",
                                                "EndDate",
                                                "Note",
                                                "Status",
                                                "Email",
                                                "NumberOfDevice",
                                                "Username"
                                            };
            object[] arrData = new object[] { 
                    OUserLogin.Fullname,
                    OUserLogin.Address,
                    OUserLogin.Phone,
                    OUserLogin.Cellphone,
                    OUserLogin.ModifyBy,
                    OUserLogin.ModifyDate,
                    OUserLogin.EndDate,
                    OUserLogin.Note,
                    OUserLogin.Status,
                    OUserLogin.Email,
                    OUserLogin.NumberOfDevice,
                    OUserLogin.Username
            };
            string result = ErpCatalogueBaseBL.Update(USERLOGIN_UPDATEGROUP, addFieldName, arrData);
            return result;
        }
        public string insert(EUserLogin OUserLogin)       {
            
            string[] addFieldName = new string[]{
                                                "Status"		,
                                                "Username"		,
                                                "Password"		,
                                                "Fullname"		,
                                                "Address"		,
                                                "Phone"			,
                                                "UserGroupID"	,
                                                "CreateBy"		,
                                                "ModifyBy"		,
                                                "CreateDate"	,
                                                "ModifyDate"	,
                                                "StartDate"		,
                                                "EndDate"		,
                                                "Note"			,
                                                "Email"			,
                                                "NumberOfDevice",
                                                "SecurityCode"	,
                                                "TimeZoneMinute",
                                                "ParentCode"	,
                                                "Cellphone"		
                                            };
            object[] arrData = new object[] { 
                    OUserLogin.Status           ,
                    OUserLogin.Username         ,
                    OUserLogin.Password         ,
                    OUserLogin.Fullname         ,
                    OUserLogin.Address          ,
                    OUserLogin.Phone            ,
                    OUserLogin.UserGroupID      ,
                    OUserLogin.CreateBy         ,
                    OUserLogin.ModifyBy         ,
                    OUserLogin.CreateDate       ,
                    OUserLogin.ModifyDate       ,
                    OUserLogin.StartDate        ,
                    OUserLogin.EndDate          ,
                    OUserLogin.Note             ,
                    OUserLogin.Email            ,
                    OUserLogin.NumberOfDevice   ,
                    OUserLogin.SecurityCode     ,
                    OUserLogin.TimeZoneMinute   ,
                    OUserLogin.ParentCode       ,
                    OUserLogin.Cellphone        
            };
            string result = ErpCatalogueBaseBL.Insert(USERLOGIN_INSERT, addFieldName, arrData);
            return result;
        }
        public int getSumDeviceBySecurityCode(string securityCode)
        {            
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(USERLOGIN_SELECTBYUSERNAME,
                                                                 new string[] { FIELD_SECURITYCODE },
                                                                 new object[] { securityCode });
            
            if (Utilities.hasRow(ds))
            {
                return (int)ds.Tables[0].Rows[0][0];
                
            }
            return 0;
        }
        public int getNumDeviceBySecurityCode(string securityCode)
        {
            List<EUserLogin> lstUser = getUserLoginBySecurityCode(securityCode);
            if (lstUser != null  && lstUser.Count>0)
            {
                return lstUser[0].NumberOfDevice;
            }
            return 0;
        }
        public int deleteUserByUsername(String userName, String type)
        {

            int result = 0;
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(USERLOGIN_DELETEUSER,
                                                            new string[] { FIELD_USERNAME, "Type" },
                                                            new object[] { userName, type });
          
            
            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                if (dt.Rows[0][0] != DBNull.Value)
                {
                    result = (int)dt.Rows[0][0];
                }
            }
            return result;
        }

        // Delete multiple user. Return 0 if fail
        public int deleteMultipUserByUsername(String userName, String type)
        {

            int result = 0;
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(USERLOGIN_DELETEMULTIPUSER,
                                                            new string[] { FIELD_USERNAME, "Type" },
                                                            new object[] { userName, type });


            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                if (dt.Rows[0][0] != DBNull.Value)
                {
                    result = (int)dt.Rows[0][0];
                }
            }
            return result;
        }
        public string updatePassword(String securityCode, String password)
        {
            string result = ErpCatalogueBaseBL.Update(USERLOGIN_UPDATEPASSWORD, 
                                                        new string[]{
                                                                    FIELD_SECURITYCODE,
                                                                    FIELD_PASSWORD} ,
                                                        new object[]{
                                                            securityCode,
                                                            password
                                                        });
            return result;
            
        }

        public string updateUser(EUserLogin user)
        {
            string[] fieldNames = new string[]{ "Fullname",
                                                "Address",
                                                "Phone",
                                                "Cellphone",
                                                "ModifyBy",
                                                "ModifyDate",
                                                "EndDate",
                                                "Note",
                                                "Status",
                                                "Email",
                                                "NumberOfDevice",
                                                "Username",
                                                "UserGroupID"
                                          };
            object[] data = new object[] { 
                        user.Fullname,
                        user.Address,
                        user.Phone,
                        user.Cellphone,
                        user.ModifyBy,
                        user.ModifyDate,
                        user.EndDate,
                        user.Note,
                        user.Status,
                        user.Email,
                        user.NumberOfDevice,
                        user.Username,
                        user.UserGroupID
            };
            string result = ErpCatalogueBaseBL.Update(USERLOGIN_UPDATE, fieldNames,data);
            return result;
        }
        // Get all children user of administart
        public List<EUserLogin> getUserLoginByUserAdministrator()
        {
            List<EUserLogin> lstData = null;
            DataSet ds = ErpCatalogueBaseBL.SelectAllWithParameter(USERLOGIN_SELECTALLBYADMIN,
                                                                  new string[] {  },
                                                                  new object[] {  });
           
            if (Utilities.hasRow(ds))
            {
                DataTable dt = ds.Tables[0];
                lstData = new List<EUserLogin>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    EUserLogin user = EUserLogin.createUser(dt.Rows[i]);
                    lstData.Add(user);
                }

            }
            return lstData;
        }

        public string changeParent(string parentCode, string userName, string password)
        {
            string[] fieldNames = new string[]{ FIELD_PARENTCODE,
                                                FIELD_USERNAME,
                                                FIELD_PASSWORD
                                          };
            object[] data = new object[] { 
                        parentCode,
                        userName,
                        password
            };
            string result = ErpCatalogueBaseBL.Update(USERLOGIN_CHANGEPARENT, fieldNames, data);
            return result;
        }
        
    }
}
